package deliver

import "chatgpt/helper/code"

var EventDeliver *Deliver

func init() {
	EventDeliver = &Deliver{
		Events: []Event{},
	}
}

type Event interface {
	Exec(map[string]any) code.CODENO
	Action() string
}

type Deliver struct {
	Events []Event
}

func (self *Deliver) Register(event Event) {
	self.Events = append(self.Events, event)
}

func (self *Deliver) Emit(data map[string]any, fn func(codeno []code.CODENO)) {
	es := []code.CODENO{}
	for _, event := range self.Events {
		e := event.Exec(data)
		if e != code.SUCCESS {
			es = append(es, e)
		}
	}
	fn(es)
}
